var koolDiscoveryContext = 'kool-discovery-portlets';
var serviceDispatcher = '../../' + koolDiscoveryContext + '/service_dispatcher_json.htm';

function GetValueQueryString (key, default_)
{
    if (default_==null) default_="";
    key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if(qs == null)
        return default_;
    else
        return qs[1];
}

function newStandbyWidget(targetName) {
    //	console.debug("	newStandbyWidget: "+targetName);
    standbyMsgBox = new dojox.widget.Standby({
        target: targetName, 
        text:"กรุณารอสักครู่ ระบบกำลังติดต่อฐานข้อมูล", 
        color:"#DDDDDD"
    });
    document.body.appendChild(standbyMsgBox.domNode);

    standbyMsgBox.startup();
    //	standbyMsgBox.show();
	
    return standbyMsgBox;
}

function FormSubmitValidate(form) {
    var formValid = form.validate();
    /*alertBox(formValid);*/
    if (!formValid) {
        alertBox("กรุณาระบุข้อมูลให้ถูกต้อง");
    }
    return formValid;
}

//for search function
//CpbCommon.js
function getQueryStringESB(val1, id1, id2){
    //return "query:"+val1+"||cpb_doc_type_sub1:"+id1+"||cpb_doc_host_org:"+id2;
    return "query:"+val1+"||cpb_doc_type_sub1:"+id1+"||cpb_doc_host_org:"+id2+"||cpb_doc_keep_status:9";
}

//fulltext.js
function isKeyEnter(e) {
    var keycode;
    if (window.event) {
        // console.debug("window.event: ");
        keycode = window.event.keyCode;
    }else if (e) {
        // console.debug("e: ");
        keycode = e.which;
    }else {
        // console.debug("else: ");
        return false;
    }
    // console.debug(keycode);
    if (keycode == 13) {
        return true;
    }else return false;
}

function bindMenu(menuId, menuStore) {
    var menu = dijit.byId(menuId);
    dojo.connect(menu, "onClick", function(item) {
        var targetPane = dijit.byId("workPane");
        var targetPage = menuStore.getValue(item, "link");
        console.log('showContent:' + targetPage);
        targetPane.set("href", targetPage);
    });
    dojo.connect(menu, "getIconClass", function(item, opened) {
        console.log('getIconClass');
        if (item && menuStore.getValue(item, "icon") != "") {
            return menuStore.getValue(item, "icon");
        } else {
            return "dijitLeaf";
        }
    });
}

function formatter1(val){
	return val.split('>>')[0];
}